<?php

namespace app\common\lib\Ai\model;

use app\common\lib\Ai\Contract;
use app\common\lib\Common;
use app\common\lib\GuzzleNew;
use app\exception\IndexException;

class ZhiZengZengOpenApi implements Contract
{
    /**
     * @throws IndexException
     */
    public function chat($content, $aiCommand = '')
    {
        //todo  分析该段描述
        $param = [
            'model' => 'gpt-4o',
            'messages' => [
                [
                    'role' => 'user',
                    'content' => $content,
                ],
            ],
        ];

        $header = [
            'Authorization' => "Bearer sk-zk2034d655b65856f64fb794e58bcf20aa766de5c3a79eec",
            'Content-Type' => 'application/json',
        ];
        $guzzle = (new GuzzleNew(300));
        $result = $guzzle->post('https://flag.smarttrot.com/', 'v1/chat/completions', $param, $header, 'json');
        //记录请求日记
        $zizengzeng_token = config('param.zizengzeng_token');
        $contentText = "\ntoken:{$zizengzeng_token}\n指令----------------------------------\n" . $aiCommand . "\nparam: ----------------------------------\n" .
            json_encode($param, 256) . "\nresult----------------------------------\n" . json_encode($result, 256) . "\n\n";
        Common::createAndSaveLog($contentText, 'zizengzeng_api');
        if (!empty($result['choices'][0]['message']['content'])) {
            return $result['choices'][0]['message']['content'];
        } elseif (!empty($result['error']['message'])) {
            throw new IndexException(4020, ($result['error']['message']));
        }
        throw new IndexException(4020, $guzzle->getError());
    }

    public function partsLongText($content, string $aiCommand = '', array $role_dict = [])
    {
        return [];
    }

    /**
     * 通过小说章节把内容分段 - 废弃
     * @param $content
     * @return mixed
     * @throws IndexException
     */
    public function generatedChapter($content)
    {

        return $this->chat($content);
    }

    /**
     * 生成中文描述词 - 废弃
     * @param $content
     * @param string $aiCommand
     * @return mixed
     * @throws IndexException
     */
    public function generatedDescWork($content, $aiCommand = '')
    {
        $aiCommand == false && $aiCommand = config('param.analyze_content_ai_command', '分析该段内容，不超过50个字：@content');

        $content = strpos($aiCommand, '@content') !== false ? str_replace(['@content'], [$content], $aiCommand) : $content;

        return $this->chat($content, $aiCommand);
    }

    /**
     * 改编文章内容
     * @param $content
     * @param string $aiCommand
     * @param string $lang
     * @return mixed
     * @throws IndexException
     */
    public function generatedContent($content, $aiCommand, $lang = '')
    {

        $content = strpos($aiCommand, '@content') !== false ? str_replace(['@content', '@lang'], [$content, $lang ?? ''], $aiCommand) : $content;

        return $this->chat($content, $aiCommand);
    }

    /**
     * 生成中文描述词
     * @param $content
     * @param string $aiCommand
     * @param string $gptVersion
     * @return mixed
     * @throws IndexException
     */
    public function generatedBatchPrompt($content, $aiCommand = '', $gptVersion = '')
    {
        $gptVersion == false && $gptVersion = config('param.default_gpt_version', 'gpt-4o');
        $aiCommand == false && $aiCommand = getAiCommand('batch_prompt_ai_command');

        $content = strpos($aiCommand, '@content') !== false ? str_replace(['@content'], [$content], $aiCommand) : $content;

        return $this->chat($content, $aiCommand);
    }

    /**
     * 生成英文描述词
     * @param $content
     * @param $aiCommand
     * @return mixed
     * @throws IndexException
     */
    public function generatedImagePrompt($content, $aiCommand = '')
    {
        $aiCommand == false && $aiCommand = config('param.prompt_ai_command', '');

        $content = strpos($aiCommand, '@content') !== false ? str_replace(['@content'], [$content], $aiCommand) : $content;

        return $this->chat($content, $aiCommand);
    }

    /**
     * 给用户文章内容通过ai分段
     * @param $content
     * @return array
     * @throws IndexException
     */
    public function aiContentPartsToUser($content)
    {
        $content = str_replace(["\n", "\r", '"', '“', '”', '[', ']', '【', '】', '「', '」', '^'], "", $content);

        $aiCommand = getAiCommand('content_parts_ai_command');
        $aiCommand = str_replace('@content', $content, $aiCommand);

        $content = $this->chat($aiCommand, $aiCommand);

        return array_values(array_filter(explode("\n", $content)));
    }
}